package itheima_edu_info_manager.com.itheima.edu.info.manager.service;

import itheima_edu_info_manager.com.itheima.edu.info.manager.dao.StudentDao;
import itheima_edu_info_manager.com.itheima.edu.info.manager.domain.Student;

public class StudentService {
    private StudentDao studentDao = new StudentDao();
    public boolean addStudent(Student student) {
        return studentDao.addStudent(student);
    }

    public Student[] findAllStudent() {
        boolean flag = false;
        Student[] allstudent = studentDao.findAllStudent();
        for (int i = 0; i < allstudent.length; i++) {
            Student student = allstudent[i];
            if (student != null){
                flag = true;
                break;
            }
        }
        if (flag){
            return allstudent;
        }else {
            return null;
        }
    }

    public boolean isExists(String id) {
        Student[] stu = studentDao.findAllStudent();
        //假设ID在数组中不存在
        boolean exists = false;
        //遍历数组，获取每个学生对象，准备进行判断
        for (int i = 0; i < stu.length; i++) {
            Student student = stu[i];
            //判断是否有null，避免空指针异常
            if (student != null && student.getId().equals(id)){
                exists = true;
                break;
            }
        }
        return exists;
    }

    public void deleteStudentById(String id) {
        studentDao.deleteStudentById(id);
    }

    public void upDateStudentById(String id, Student student) {
        studentDao.upDateStudentById(id,student);
    }
}
